package com.example.concurrentprogramming8016.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-03 1:36
 */
public class ThreadInterruptTest {
    static int i = 0;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("begin");
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    System.out.println("线程已经中断了---");
                    System.out.println("退出循环");
                    break;
                }
                i++;
                System.out.println("正在运行:" + i);
                //清除标记位
                //Thread.interrupted();
                //不会清楚清除标记位置
                if (Thread.currentThread().isInterrupted()) {
                    while (true) {
                        System.out.println("已经中断的线程:");
                        if (!Thread.currentThread().isInterrupted()) {
                            break;
                        }
                    }
                }
                if (i == 10) {
                    break;
                }
            }
        });
        thread.start();
        TimeUnit.SECONDS.sleep(3);
        thread.interrupt();
    }

}
